// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;

namespace SnapObjects.Data
{
    public class SqlBuilderFactoryBase : ISqlBuilderFactory
    {
        public virtual TSqlBuilder Create<TSqlBuilder>(string name = "")
            where TSqlBuilder : ISqlBuilder
        {
            ISqlBuilder builder = null;

            if (typeof(TSqlBuilder) == typeof(ISqlQueryBuilder))
            {
                builder = this.CreateQueryBuilder(name);
            }
            else if (typeof(TSqlBuilder) == typeof(ISqlInsertBuilder))
            {
                builder = this.CreateInsertBuilder(name);
            }
            else if (typeof(TSqlBuilder) == typeof(ISqlDeleteBuilder))
            {
                builder = this.CreateDeleteBuilder(name);
            }
            else if (typeof(TSqlBuilder) == typeof(ISqlUpdateBuilder))
            {
                builder = this.CreateUpdateBuilder(name);
            }
            else if (typeof(TSqlBuilder) == typeof(ISqlProcedureBuilder))
            {
                builder = this.CreateProcedureBuilder(name);
            }
            else if (typeof(TSqlBuilder) == typeof(ISqlRawBuilder))
            {
                builder = this.CreateRawBuilder(name);
            }
            else
            {
                throw new NotSupportedException(typeof(TSqlBuilder).FullName);
            }

            return (TSqlBuilder)builder;
        }

        protected virtual ISqlBuilder CreateQueryBuilder(string name)
        {
            return new SqlQueryBuilder(name);
        }

        protected virtual ISqlBuilder CreateInsertBuilder(string name)
        {
            return new SqlInsertBuilder(name);
        }

        protected virtual ISqlBuilder CreateDeleteBuilder(string name)
        {
            return new SqlDeleteBuilder(name);
        }

        protected virtual ISqlBuilder CreateUpdateBuilder(string name)
        {
            return new SqlUpdateBuilder(name);
        }

        protected virtual ISqlBuilder CreateProcedureBuilder(string name)
        {
            return new SqlProcedureBuilder(name);
        }

        protected virtual ISqlBuilder CreateRawBuilder(string name)
        {
            return new SqlRawBuilder(name);
        }
    }
}
